#!/bin/bash

version=3.9.20
head_version=$(echo ${version} | awk -F '.' '{ print $1 "." $2 }')
date_now=$(date +%Y%m%d)

yum install -y zlib-devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make openssl openssl-devel epel-release libffi libffi-devel python-pip wget
mv /usr/bin/python /usr/bin/python.bak_${date_now}
wget https://www.python.org/ftp/python/${version}/Python-${version}.tar.xz
xz -d Python-${version}.tar.xz
tar -xf Python-${version}.tar
cd Python-${version} && ./configure prefix=/usr/local/python3
make && make install
ln -sv /usr/local/python3/bin/python${head_version} /usr/bin/python
ln -sv /usr/local/python3/bin/pip3 /usr/bin/pip3

sed -i 's/#!\/usr\/bin\/python/#!\/usr\/bin\/python2/g' /usr/bin/yum
sed -i 's/#! \/usr\/bin\/python/#! \/usr\/bin\/python2/g' /usr/libexec/urlgrabber-ext-down
sed -i 's/#!\/usr\/bin\/python -Es/#!\/usr\/bin\/python2.7 -Es/g' /usr/bin/firewall-cmd
sed -i 's/#!\/usr\/bin\/python -Es/#!\/usr\/bin\/python2.7 -Es/g' /usr/sbin/firewalld


cur_version=$(python -V | awk '{ print $2 }')
if [ $cur_version = $version ];then
  echo -e "python${version} install succeed !"
else
  echo -e "python${version} install failed !"
fi
